#include<iostream>
#include<unordered_map>
#include<vector>
#include<unordered_set>
#include<algorithm>
#include<map>
using namespace std;


void solve()
{
	vector<int> arr;
	unordered_map<int, int> ma;
	int n, k; cin >> n >> k;
	for (int i = 0; i < k; i++)
	{
		int a, b; cin >> a >> b;
		ma[a] += b;
	}
	for (auto& x : ma)
	{
		arr.push_back(x.second);
	}
	sort(arr.begin(), arr.end(), [&](int a, int b) {return a > b; });
	int ans = 0;
	for (int i = 0; i < min(n, (int)arr.size()); i++) ans += arr[i];
	cout << ans << endl;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}
